home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / win / vbsmpls.zip / SAMPLES / OLEDB / OLEDB.FRM < prev    next >
Text File  |  1994-03-24  |  12KB  |  390 lines

  1. VERSION 2.00
  2. Begin Form frmObjVwr 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "OLEDB"
  5.    ClientHeight    =   5415
  6.    ClientLeft      =   900
  7.    ClientTop       =   2340
  8.    ClientWidth     =   7710
  9.    Height          =   5820
  10.    Icon            =   OLEDB.FRX:0000
  11.    Left            =   840
  12.    LinkTopic       =   "Form1"
  13.    ScaleHeight     =   5415
  14.    ScaleWidth      =   7710
  15.    Top             =   1995
  16.    Width           =   7830
  17.    Begin CommandButton cmdDelete 
  18.       Caption         =   "&Delete Current Object"
  19.       Height          =   612
  20.       Left            =   3840
  21.       TabIndex        =   4
  22.       Top             =   4680
  23.       Width           =   2052
  24.    End
  25.    Begin CommandButton cmdNext 
  26.       Caption         =   "&Next >>"
  27.       Height          =   492
  28.       Left            =   3840
  29.       TabIndex        =   2
  30.       Top             =   3960
  31.       Width           =   2052
  32.    End
  33.    Begin CommandButton cmdPrevious 
  34.       Caption         =   "<< &Previous"
  35.       Height          =   492
  36.       Left            =   1680
  37.       TabIndex        =   1
  38.       Top             =   3960
  39.       Width           =   2052
  40.    End
  41.    Begin OLE oleDisplay 
  42.       fFFHk           =   -1  'True
  43.       Height          =   3492
  44.       Left            =   120
  45.       TabIndex        =   0
  46.       Top             =   360
  47.       Width           =   7332
  48.    End
  49.    Begin CommandButton cmdAddNew 
  50.       Caption         =   "&Add New OLE Object"
  51.       Height          =   612
  52.       Left            =   1680
  53.       TabIndex        =   3
  54.       Top             =   4680
  55.       Width           =   2052
  56.    End
  57.    Begin Label lblFormatInfo 
  58.       BackStyle       =   0  'Transparent
  59.       Height          =   252
  60.       Left            =   120
  61.       TabIndex        =   5
  62.       Top             =   120
  63.       Width           =   5532
  64.    End
  65. End
  66. '******************************************************************'
  67. '*                                                                *'
  68. '*  OLEDB - Database Storage and Retrieval of OLE Objects         *'
  69. '*                                                                *'
  70. '*  OLEDB.FRM - Front end to display objects and allow the        *'
  71. '*              user to modify the database.                      *'
  72. '*                                                                *'
  73. '*  OLEDB.BAS - Routines that store and retrieve objects.         *'
  74. '*            - Reusable code that can store OLE2 objects         *'
  75. '*              into binary or memo field, retrieve OLE2          *'
  76. '*              objects, and retrieve Access 1.x format OLE       *'
  77. '*              objects.                                          *'
  78. '*            - If an OLE object is added from within Access      *'
  79. '*              the Format field should be set to 1.              *'
  80. '*            - There is not a routine to store Access 1.x        *'
  81. '*              format OLE objects.                               *'
  82. '*                                                                *'
  83. '*  OLEDB.MDB - Access database to hold objects                   *'
  84. '*            - Structure:                                        *'
  85. '*               TABLES: OLEObjects                               *'
  86. '*               FIELDS: OLE_ID     Counter                       *'
  87. '*                       OLEObject  Binary                        *'
  88. '*                       Format     Long                          *'
  89. '*              INDEXES: Index OLE_ID Unique Primary              *'
  90. '*                                                                *'
  91. '******************************************************************'
  92.  
  93. Option Explicit
  94.  
  95. Dim dbOLEDB As Database
  96. Dim tbOLEObjects As Dynaset
  97. Dim nRecordCount As Integer
  98. Dim nRecordNumber As Integer
  99. Dim bBusy As Integer            'Used to prevent re-entry
  100. Dim bUpdated As Integer         'Flags that the object has been updated
  101.  
  102. Const OLE2OBJECT = 0            'Program defined constants for
  103. Const ACCESSOLE1OBJECT = 1      'Format field
  104.  
  105. Const OLE_DELETE = 10           'OLE2 control actions
  106. Const OLE_INSERT_OBJ_DLG = 14
  107.  
  108. Const OLE_CHANGED = 0             'OLE updated event codes
  109. Const OLE_SAVED = 1
  110.  
  111. Const MB_YESNO = 4              'Message Box constants
  112. Const IDYES = 6
  113.  
  114. 'Adds a new Object to the database
  115. '
  116. Sub cmdAddNew_Click ()
  117. If Not bBusy Then
  118.    bBusy = True
  119.    Screen.MousePointer = 11
  120.    DoEvents
  121.    Dim eError As Integer
  122.    If bUpdated Then
  123.       Call PutOLEObject
  124.       DoEvents
  125.       bUpdated = False
  126.    End If
  127.    oleDisplay.Action = OLE_DELETE
  128.    oleDisplay.OLETypeAllowed = 1           'Limit to Embedded objects
  129.    On Error GoTo INSERTERROR:
  130.    oleDisplay.Action = OLE_INSERT_OBJ_DLG
  131.    On Error GoTo 0
  132.    DoEvents
  133.    If oleDisplay.OLEType = 3 Then
  134.       If nRecordNumber <> 0 Then
  135.          Call GetOLEObject
  136.       End If
  137.    Else
  138.       tbOLEObjects.AddNew
  139.       tbOLEObjects("Format") = OLE2OBJECT
  140.       eError = OLEToField(oleDisplay, tbOLEObjects("oleobject"))
  141.       nRecordCount = nRecordCount + 1
  142.       nRecordNumber = nRecordCount
  143.       lblFormatInfo.Caption = nRecordNumber & " of " & nRecordCount & ": Stored as OLE2 Object"
  144.       tbOLEObjects.Update
  145.       tbOLEObjects.Bookmark = tbOLEObjects.LastModified
  146.       Call UpdateButtons
  147.       DoEvents
  148.    End If
  149. GoTo EXITADDNEW:
  150. INSERTERROR:
  151.    MsgBox ("The object could not be created.  Try to free up more memory.")
  152.    If nRecordNumber <> 0 Then
  153.       Call GetOLEObject
  154.    End If
  155.    Resume EXITADDNEW:
  156. EXITADDNEW:
  157.    Screen.MousePointer = 0
  158.    bBusy = False
  159. End If
  160. End Sub
  161.  
  162. 'Deletes the current object from the database
  163. '
  164. Sub cmdDelete_Click ()
  165.    If Not bBusy Then
  166.       bBusy = True
  167.       Screen.MousePointer = 11
  168.       DoEvents
  169.       If nRecordNumber <> 0 Then
  170.          oleDisplay.Action = OLE_DELETE
  171.          tbOLEObjects.Delete
  172.          If nRecordNumber <> nRecordCount Then
  173.             tbOLEObjects.MoveNext
  174.             nRecordCount = nRecordCount - 1
  175.             Call GetOLEObject
  176.             Call UpdateButtons
  177.             DoEvents
  178.          Else
  179.             If nRecordNumber = 1 Then
  180.                nRecordCount = 0
  181.                nRecordNumber = 0
  182.                Call UpdateButtons
  183.                lblFormatInfo.Caption = nRecordNumber & " of " & nRecordCount & ": No Current Record"
  184.             Else
  185.                tbOLEObjects.MoveLast
  186.                nRecordCount = nRecordCount - 1
  187.                nRecordNumber = nRecordCount
  188.                Call GetOLEObject
  189.                Call UpdateButtons
  190.                DoEvents
  191.             End If
  192.          End If
  193.       End If
  194.       bUpdated = False
  195.       Screen.MousePointer = 0
  196.       bBusy = False
  197.    End If
  198. End Sub
  199.  
  200. 'Moves to the next object in the database
  201. '
  202. Sub cmdNext_Click ()
  203.    If Not bBusy Then
  204.       bBusy = True
  205.       Screen.MousePointer = 11
  206.       DoEvents
  207.       If bUpdated Then
  208.          Call PutOLEObject
  209.          DoEvents
  210.          bUpdated = False
  211.       End If
  212.       If nRecordNumber <> nRecordCount Then
  213.          tbOLEObjects.MoveNext
  214.          nRecordNumber = nRecordNumber + 1
  215.          Call GetOLEObject
  216.          Call UpdateButtons
  217.          DoEvents
  218.       End If
  219.       Screen.MousePointer = 0
  220.       bBusy = False
  221.    End If
  222. End Sub
  223.  
  224. 'Moves to the previous object in the database
  225. '
  226. Sub cmdPrevious_Click ()
  227.    If Not bBusy Then
  228.       bBusy = True
  229.       Screen.MousePointer = 11
  230.       DoEvents
  231.       If bUpdated Then
  232.          Call PutOLEObject
  233.          DoEvents
  234.          bUpdated = False
  235.       End If
  236.       If nRecordNumber > 1 Then
  237.          tbOLEObjects.MovePrevious
  238.          nRecordNumber = nRecordNumber - 1
  239.          Call GetOLEObject
  240.          Call UpdateButtons
  241.          DoEvents
  242.       End If
  243.       Screen.MousePointer = 0
  244.       bBusy = False
  245.    End If
  246. End Sub
  247.  
  248. 'Open up the database and move to the first object
  249. '
  250. 'NOTE: No error trapping on the opening the database or table
  251. '
  252. Sub Form_Load ()
  253.    
  254.    Dim dyOLEObjects As Dynaset
  255.    
  256.    frmObjVwr.Top = (Screen.Height - frmObjVwr.Height) / 2
  257.    frmObjVwr.Left = (Screen.Width - frmObjVwr.Width) / 2
  258.    Set dbOLEDB = OpenDatabase(App.Path & "\OLEDB.MDB")
  259.    Set tbOLEObjects = dbOLEDB.CreateDynaset("OLEObjects")
  260.    
  261.    On Error GoTo NOCURRECLOAD:
  262.    tbOLEObjects.MoveLast
  263.    tbOLEObjects.MoveFirst
  264.    On Error GoTo 0
  265.    nRecordCount = tbOLEObjects.RecordCount
  266.    nRecordNumber = 1
  267.    Call GetOLEObject
  268.    Call UpdateButtons
  269. Exit Sub
  270. NOCURRECLOAD:
  271.    nRecordCount = 0
  272.    nRecordNumber = 0
  273.    Call UpdateButtons
  274.    lblFormatInfo.Caption = nRecordNumber & " of " & nRecordCount & ": No Current Record"
  275.    Resume EXITSUBLOAD:
  276. EXITSUBLOAD:
  277. End Sub
  278.  
  279. Sub Form_QueryUnload (Cancel As Integer, UnloadMode As Integer)
  280.    If bUpdated Then
  281.       Call PutOLEObject
  282.       DoEvents
  283.       bUpdated = False
  284.    End If
  285. End Sub
  286.  
  287. 'Sizes controls to fit form
  288. '
  289. Sub Form_Resize ()
  290. If frmObjVwr.WindowState <> 1 Then
  291.    If frmObjVwr.ScaleWidth > 4300 Then
  292.       oleDisplay.Width = frmObjVwr.ScaleWidth - 240
  293.       cmdPrevious.Left = frmObjVwr.ScaleWidth \ 2 - 2118
  294.       cmdNext.Left = frmObjVwr.ScaleWidth \ 2 + 66
  295.       cmdAddNew.Left = frmObjVwr.ScaleWidth \ 2 - 2118
  296.       cmdDelete.Left = frmObjVwr.ScaleWidth \ 2 + 66
  297.       
  298.    End If
  299.    If frmObjVwr.Height > 3000 Then
  300.       oleDisplay.Height = frmObjVwr.ScaleHeight - 2064
  301.       cmdPrevious.Top = frmObjVwr.ScaleHeight - 1488
  302.       cmdNext.Top = frmObjVwr.ScaleHeight - 1488
  303.       cmdAddNew.Top = frmObjVwr.ScaleHeight - 804
  304.       cmdDelete.Top = frmObjVwr.ScaleHeight - 804
  305.    End If
  306. End If
  307. End Sub
  308.  
  309. 'Determines the format of the Object and calls the appropriate
  310. 'function to retrieve it
  311. '
  312. 'NOTE: eError will never recieve an error unless an error trapping
  313. 'scheme is implemented in OLEDB.BAS
  314. '
  315. Sub GetOLEObject ()
  316.    Dim eError As Integer
  317.    If tbOLEObjects("format") = OLE2OBJECT Then
  318.       eError = FieldToOLE(oleDisplay, tbOLEObjects("oleobject"))
  319.       lblFormatInfo.Caption = nRecordNumber & " of " & nRecordCount & ": Stored as OLE2 Object"
  320.    Else 'ACCESSOLE1OBJECT
  321.       eError = AccessFieldToOLE(oleDisplay, tbOLEObjects("oleobject"))
  322.       lblFormatInfo.Caption = nRecordNumber & " of " & nRecordCount & ": Stored as Access 1.x OLE Object"
  323.    End If
  324. End Sub
  325.  
  326. 'Sets flag to update the current object in the database if the user edits it
  327. '
  328. Sub oleDisplay_Updated (Code As Integer)
  329.    If Not bBusy Then
  330.       bBusy = True
  331.       If Code = OLE_SAVED Then
  332.          Screen.MousePointer = 11
  333.          PutOLEObject
  334.          bUpdated = False
  335.          Screen.MousePointer = 0
  336.       ElseIf Code = OLE_CHANGED Then
  337.          bUpdated = True
  338.       End If
  339.       bBusy = False
  340.    End If
  341. End Sub
  342.  
  343. Sub PutOLEObject ()
  344.    Dim eError As Integer
  345.    If tbOLEObjects("format") = OLE2OBJECT Then
  346.       tbOLEObjects.Edit
  347.       eError = OLEToField(oleDisplay, tbOLEObjects("oleobject"))
  348.       tbOLEObjects("format") = OLE2OBJECT
  349.       lblFormatInfo.Caption = nRecordNumber & " of " & nRecordCount & ": Stored as OLE2 Object"
  350.       tbOLEObjects.Update
  351.       DoEvents
  352.    Else 'OLE1ACCESSOBJECT
  353.       If MsgBox("This program cannot update Access 1.x OLE Objects.  Do you want to save your changes as an OLE2 Object?", MB_YESNO) = IDYES Then
  354.          tbOLEObjects.Edit
  355.          eError = OLEToField(oleDisplay, tbOLEObjects("oleobject"))
  356.          tbOLEObjects("format") = OLE2OBJECT
  357.          lblFormatInfo.Caption = nRecordNumber & " of " & nRecordCount & ": Stored as OLE2 Object"
  358.          tbOLEObjects.Update
  359.          DoEvents
  360.       Else
  361.          GetOLEObject
  362.       End If
  363.    End If
  364. End Sub
  365.  
  366. Sub UpdateButtons ()
  367.    If nRecordCount = 0 Then
  368.       cmdNext.Enabled = False
  369.       cmdPrevious.Enabled = False
  370.       cmdDelete.Enabled = False
  371.    ElseIf nRecordCount = 1 Then
  372.       cmdNext.Enabled = False
  373.       cmdPrevious.Enabled = False
  374.       cmdDelete.Enabled = True
  375.    ElseIf nRecordNumber = nRecordCount Then
  376.       cmdNext.Enabled = False
  377.       cmdPrevious.Enabled = True
  378.       cmdDelete.Enabled = True
  379.    ElseIf nRecordNumber = 1 Then
  380.       cmdNext.Enabled = True
  381.       cmdPrevious.Enabled = False
  382.       cmdDelete.Enabled = True
  383.    Else
  384.       cmdNext.Enabled = True
  385.       cmdPrevious.Enabled = True
  386.       cmdDelete.Enabled = True
  387.    End If
  388. End Sub
  389.  
  390.